from datetime import datetime
import coin.exchange.base.kr_rest.native_public_client_base as npubcb


class DydxFuturesNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self):
    npubcb.NativePublicClientBase.__init__(self, url="https://api.dydx.exchange")
    self._valid_kline_period = [
        '1MIN', '5MINS', '15MINS', '30MINS', '1HOUR', '4HOURS', '12HOURS', '1DAY'
    ]

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, 'Invalid kline period!'

  def get_history_kline(self, symbol, kline_period, limit=None, start_time=None, end_time=None):
    self.check_kline_period(kline_period)
    url = "v3/candles/" + symbol
    params = {
        'resolution': kline_period,
        'limit': limit,
        'fromISO': datetime.strftime(start_time, '%Y-%m-%dT%H%M%S') if start_time else None,
        'toISO': datetime.strftime(end_time, '%Y-%m-%dT%H%M%S') if end_time else None
    }
    return self._get_as_json(url, params)

  def queryAll(self):
    raise NotImplementedError()
